﻿using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
using System.Drawing;

namespace QExplorer
{
	class QTree : TreeView
	{
		private QForm form;

		public QForm Form
		{
			get { return form; }
			set { form = value; }
		}

		public QTree()
			: base()
		{
			InitializeComponent();

		}
		private void InitializeComponent()
		{
			this.SuspendLayout();
			// 
			// QTree
			// 
			this.AllowDrop = true;
			this.Font = new System.Drawing.Font("微软雅黑", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
			this.LineColor = System.Drawing.Color.Black;
			this.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.QTree_BeforeExpand);
			this.NodeMouseHover += new System.Windows.Forms.TreeNodeMouseHoverEventHandler(this.QTree_NodeMouseHover);
			this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.QTree_MouseUp);
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.QTree_DragDrop);
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.QTree_MouseDown);
			this.DragEnter += new System.Windows.Forms.DragEventHandler(this.QTree_DragEnter);
			this.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.QTree_NodeMouseClick);
			this.ResumeLayout(false);

		}

		public void SetTree()
		{
			DriveInfo[] drives = DriveInfo.GetDrives();

			SHFILEINFO sfi = new SHFILEINFO();
			foreach (DriveInfo drive in drives)
			{
				if (drive.IsReady)
				{
					TreeNode node = new TreeNode(drive.Name);
					node.Name = drive.Name;
					API.GetFileInfo(drive.Name, ref sfi, API.SHGFI_SYSICONINDEX);
					node.ImageIndex = sfi.iIcon;
					API.GetFileInfo(drive.Name, ref sfi, API.SHGFI_SYSICONINDEX | API.SHGFI_OPENICON);
					node.SelectedImageIndex = sfi.iIcon;
					node.Nodes.Add("...");
					Nodes.Add(node);
				}
			}
		}

		private void QTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
		{
			#region 判断节点是否已经展开
			if (e.Node.Nodes.Count != 1)
			{
				return;
			}
			else
			{
				if (e.Node.FirstNode.Text != "...")
				{
					return;
				}
			}

			e.Node.Nodes.Clear();
			#endregion

			SHFILEINFO sfi = new SHFILEINFO();
			string[] folders = Directory.GetDirectories(e.Node.Name);
			foreach (string dir in folders)
			{
				if (!form.CanShowFile(dir))
				{
					continue;
				}
				TreeNode node = new TreeNode(Path.GetFileName(dir));
				node.Name = dir;
				API.GetFileInfo(dir, ref sfi, API.SHGFI_SYSICONINDEX);
				node.ImageIndex = sfi.iIcon;
				API.GetFileInfo(dir, ref sfi, API.SHGFI_SYSICONINDEX | API.SHGFI_OPENICON);
				node.SelectedImageIndex = sfi.iIcon;
				try
				{
					if (Directory.GetDirectories(dir).Length > 0)
					{
						node.Nodes.Add("...");
					}
				}
				catch (UnauthorizedAccessException ex)
				{
					Debug.WriteLine(ex.ToString());
				}
				e.Node.Nodes.Add(node);
			}
		}
		private void QTree_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				SelectedNode = GetNodeAt(e.X, e.Y);
			}
		}

		private void QTree_MouseUp(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				if (SelectedNode != null)
				{
				}
			}
		}

		private void QTree_DragDrop(object sender, DragEventArgs e)
		{
			TreeNode node = this.GetNodeAt(PointToClient(new Point(e.X, e.Y)));
			if (node == null)
			{
				return;
			}
			string path = (string)e.Data.GetData(typeof(string));
			string newPath = Path.Combine(node.Name, Path.GetFileName(path));
			try
			{
				Directory.Move(path, newPath);
			}
			catch (IOException ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void QTree_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = e.AllowedEffect;
		}

		private void QTree_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
		{
			SelectedNode = e.Node;
		}

		private void QTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			form.Open(e.Node.Name);
			form.OnActiveView();
		}

	}
}
